package com.Tools.TestDb.Service;

import com.Tools.TestDb.Entity.TestDb;
import com.Tools.TestDb.Mapper.TestDbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.utils.APIRespones;
@Service
public class TestDbService {
    @Autowired
    private TestDbMapper testDbMapper;

    // 获取指定数量的题目
    public APIRespones getTestDbByBankTypeRandom(String bankType, int count) {
        List<TestDb> testDbList = testDbMapper.getTestDbByBankTypeRandom(bankType, count);

        if (testDbList == null || testDbList.size() == 0) {
            return APIRespones.failure("未找到指定类型的试题");
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("testDbList", testDbList);

        return APIRespones.success(resultMap);
    }

    public TestDb  updateTestDbScoreById(Long id, int score) {
            TestDb testDb = testDbMapper.selectById(id);
            if (testDb == null) {
                return null;
            }

            testDb.updateScore(score);
            testDbMapper.updateTestDbScoreById(testDb.getId(), testDb.getAnswerCount(), testDb.getAverageScore(), testDb.getMastery());

            return testDb;

    }
}